package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 仪器信息网
 * @author Persh
 *
 */
public class InstrumentNewsAnalyse implements AnalyseNews {
	private static final String[] regex={
		"http://www.instrument.com.cn/news/[0-9]{8}/[0-9]*.shtml",
		"http://www.instrument.com.cn/news/newhotcount.asp\\?id=[0-9]*&ref=.*&tab=1",
		"http://www.instrument.com.cn/netshow/SH100343/news_[0-9]*.htm\\?ref=",
	};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		news.setUrl(url);		
		String title=null;
		String content=null;
		Long date=null;
		Integer clickNum=0;
		
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h1.F24").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("h1.h1title").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.G4"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.ft14"));
			if (StringUtils.isBlank(content)) {
				content=HtmlCleaner.getContentHtml(url,doc.select("div.ContL"));
			}
		}
		
		date=DateUtils.matchDate(doc.select("div.G9").text());
		if(date == null){
			date=DateUtils.matchDate(doc.select("h1.h1title").text());	
		}
		
		String clickStr=doc.select("div.G9").text();
		if(StringUtils.isBlank(clickStr)){
			clickStr=doc.select("h1.h1title").text();
			if(clickStr.indexOf("点击") != -1 && clickStr.indexOf("次") != -1){		
				clickStr=StringUtils.substringBetween(clickStr, "点击", "次").trim();
			}else if(clickStr.indexOf("点击：") != -1 && clickStr.indexOf("）") != -1){
				clickStr=StringUtils.substringBetween(clickStr, "点击：", "）");
			}
		}

		try {
			clickNum=Integer.parseInt(clickStr);
		} catch (Exception e) {
			// TODO: handle exception
		}
		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);	
		news.setClickNum(clickNum);
		return news;
	}
	
	public static void main(String[] args) {
		InstrumentNewsAnalyse analyse=new InstrumentNewsAnalyse();
		String url="http://www.instrument.com.cn/netshow/SH100815/s271822.htm";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
